package com.mobi.cacti.rabbit.controllers;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.holoeverywhere.app.Fragment;



import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.mobi.cacti.model.mdRespListOfDatas;
import com.mobi.cacti.rabbit.connectors.AbstractServerConnector;
import com.mobi.cacti.rabbit.connectors.LoginActivityServerConnector;
import com.mobi.cacti.rabbit.connectors.MainInfoActivityServerConnector;
import com.mobi.cacti.rabbit.connectors.ServerConnectorCallback;
import com.mobi.cacti.rabbit.connectors.TicketCountServerConnector;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.CustomerInfoCurcitData;
import com.mobi.cacti.rabbit.response.ReqMobileLogin;
import com.mobi.cacti.rabbit.response.ReqCustomerServiceInfo;
import com.mobi.cacti.rabbit.response.UserInfo;
import com.mobi.cacti.rabbit.utils.SAXXMLParser;


public class MainInfoActivityController implements ServerConnectorCallback{
	
	private MainInfoActivityServerConnector loadCustomerServiceInfoServerConnector;
	
	public MainInfoActivityControllerCallback delegate;
	private static MainInfoActivityController instance = null;

	public static MainInfoActivityController getInstance() {
		if(instance == null) {
			instance = new MainInfoActivityController();
		}
		return instance;
	}
	
	public void loadCustomerServiceInfo(Context context)
	{

		if(!Cache.isNetworkAvailable(context))
		{
			Cache.showNoInternetAlertDialog(context);
			return ;
		}

	
		loadCustomerServiceInfoServerConnector = new MainInfoActivityServerConnector();
		loadCustomerServiceInfoServerConnector.delegate = this;
		
		String[] curcitIdArray = new String[Cache.reqMobileLogin.responseCircuitIDWhenLogin.keySet().toArray().length];
		curcitIdArray = (String[]) Cache.reqMobileLogin.responseCircuitIDWhenLogin.keySet().toArray(curcitIdArray);

		loadCustomerServiceInfoServerConnector.loadCustomerServiceInfo(Cache.userInfo.UserName, curcitIdArray);
	}

	@Override
	public void serverConnectorFinishBackgroundTask(AbstractServerConnector connector,Object response) {

		Log.i("tai", "HTTPRESPONSE = "+response);
		
		if(connector instanceof MainInfoActivityServerConnector)
		{
			InputStream is = new ByteArrayInputStream(((String)response).getBytes());
			ReqCustomerServiceInfo obj = SAXXMLParser.respW4CusInfo(is);
			Cache.reqW4CusInfo = obj;

//			Log.i("tai", "ComTel = "+Cache.reqW4CusInfo.ComTel);
//			Log.i("tai", "ComEmail = "+Cache.reqW4CusInfo.ComEmail);
//			Log.i("tai", "contactPerEmail = "+ ((CustomerInfoCurcitData)(Cache.reqW4CusInfo.customerInfoCurcitDataMap.values().toArray()[1])).contactPerEmail );
//			Log.i("tai", "contactPerName = "+ ((CustomerInfoCurcitData)(Cache.reqW4CusInfo.customerInfoCurcitDataMap.values().toArray()[1])).contactPerName );
//			Log.i("tai", "contactPerTel = "+ ((CustomerInfoCurcitData)(Cache.reqW4CusInfo.customerInfoCurcitDataMap.values().toArray()[1])).contactPerTel );
			
			this.delegate.mainInfoActivityControllerCompleteLoadCustomerServiceInfo();
		}
		
	}


}
